/**
 * @author:albert
 */

package com.bkbw.live.battle.business.ai;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import scala.annotation.varargs;
import com.bkbw.live.battle.map.MapAStarInfo;

import com.badlogic.gdx.ai.btree.Task;
import com.badlogic.gdx.ai.btree.Task.Status;
import com.badlogic.gdx.ai.msg.Telegram;
import com.badlogic.gdx.math.MathUtils;

import com.bkbw.live.common.GameConfig;
import com.bkbw.live.common.notifycenter.MessageDefine;
import com.bkbw.live.common.GameConfig.UnitMoveMode;
import akka.event.Logging.Debug;
import com.bkbw.live.battle.business.UnitObject;

public class BtRandomMove<EntityType extends UnitObject> extends BaseLeafNode<EntityType> {

	List<Vector3D> paths;
	boolean stopMove = false;
	boolean userControlMove = false;
	Vector3D nextPoint;
	
	Logger logger = LoggerFactory.getLogger(GameConfig.LoggerName);
	
	@Override
	public void start() 
	{
		EntityType entity = getObject();
		entity.setMoveMode(UnitMoveMode.Target);
		if (paths == null || paths.size() == 0) {
			MapAStarInfo astarinfo = entity.getMap().getAstarInfo();
			int repeat = 5;
			int posx = 0;
			int posz = 0;
			while (true) {
				if (repeat == 0) {
					logger.debug("random move can not find path");
					return;
				}
				posx = MathUtils.random(0, astarinfo.getMaxX() - 1);
				posz = MathUtils.random(0, astarinfo.getMaxZ() - 1);
				if (astarinfo.canCross(posx, posz)) {
					break;
				}
				repeat--;
			}
			Vector3D to = new Vector3D(posx, 0.0D, posz);
			//paths = entity.getMap().getAstarInfo().findPath(entity.getPosition(), to);
			paths = new LinkedList<Vector3D>();
			paths.add(entity.getPosition());
			paths.add(to);
			if (paths == null || paths.size() == 0) {
				logger.debug("random move no path find");
				logger.debug("random move start from " + entity.getPosition());
				logger.debug("random move end at " + to);
				return;
			}
			
			logger.debug("random move start from " + entity.getPosition());
			if (paths != null && paths.size() > 0) 
				logger.debug("random move end at " + paths.get(paths.size() - 1));
//			logger.debug("random move find path begin----------");
//			for (Vector3D v : paths) {
//				logger.debug(""+v);
//			}
//			logger.debug("random move find path end----------");
		}
	
		if (paths != null && paths.size() > 0) {
			entity.setMoving(true);
			nextPoint = paths.get(0);
			entity.setMoveTarget(nextPoint);
			logger.debug("random move set move target " + nextPoint);
			paths.remove(0);
		}
	}

	@Override
	public com.badlogic.gdx.ai.btree.Task.Status execute() {
		// TODO Auto-generated method stub
		// 寻路失败
		if (paths == null) {
			logger.debug("random move no path");
			return Status.FAILED;
		}
		
		// 用户控制
		if (userControlMove) {
			logger.debug("random move user control");
			return Status.FAILED;
		}
		
		// 行进中不知道碰到了什么，停止行走了
		if (stopMove) {
			logger.debug("random move stop move");
			return Status.FAILED;
		}
		
		EntityType entity = getObject();
//		if (entity.isTargetAttackable()) {
//			// 追击中怪物进入攻击范围，结束追击
//			if (entity.isTargetInDistance()) {
//				return Status.SUCCEEDED;
//			}
//		}
//		// 当前追寻的怪物被干掉了，需要重新寻找目标
//		else {
//			return Status.FAILED;
//		}
		
		if (entity.isAtTarget(nextPoint)) {
			// 到达了路点的下一个目标，如果是最后一步
			if (paths.size() == 0) {
				logger.debug("get destination");
				return Status.SUCCEEDED;
			}
			else {
				logger.debug("next edge");
				start();
				return Status.RUNNING;
			}
		}
		else {
			
			return Status.RUNNING;
		}
	}
	
	@Override
	public void end() 
	{
		paths = null;
		stopMove = false;
		EntityType entity = getObject();
		if (!userControlMove) {
			entity.setMoving(false);
		}
		userControlMove = false;
		logger.debug("random move end");
	}

	@Override
	protected Task<EntityType> copyTo(Task<EntityType> paramTask) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean handleMessage(Telegram paramTelegram) {
		// TODO Auto-generated method stub
		return false;
	}

}
